package display
{
	import flash.display.BitmapData;
	import flash.utils.Dictionary;
	
	/**
	 * 位图缓存管理器
	 * @author gcxk721
	 * 2012-6-19
	 * 上午11:00:06
	 */
	public class QhBitmapSaveManager
	{
		
		/**
		 * 角色资源缓存 
		 */
		private var roleDictionary : Dictionary = new Dictionary(true);
		
		/**
		 * 效果资源缓存 
		 */
		private var effectDictionary : Dictionary = new Dictionary(true);

		private static var _instance : QhBitmapSaveManager = new QhBitmapSaveManager();
		
		public function QhBitmapSaveManager()
		{
			if(_instance != null)
			{
				throw new Error("single instance");
			}
		}
		
		/**
		 * 缓存特效资源 
		 * @param key
		 * @param value Vector.<QhBitmap>    or    Vector.<Vector.<QhBitmap>>
		 * 
		 */
		public function cacheEffectBitmap(key : String, value : *) : void
		{
			effectDictionary[key] = value;
		}
		
		/**
		 * 取得缓存的特效资源 
		 * @param key
		 * @return Vector.<QhBitmap>    or    Vector.<Vector.<QhBitmap>>
		 * 
		 */
		public function getEffectBitmap(key : String) : *
		{
			return effectDictionary[key];
		}
		
		/**
		 * 缓存临时的位图数据 
		 * @param key
		 * @param value
		 * 
		 */
		public function cacheRoleBitmap(resId : int, value : Vector.<Vector.<Vector.<QhBitmap>>>) : void
		{
			roleDictionary[resId] = value;
		}
		
		/**
		 * 取得缓存的人物效果资源 
		 * @param key
		 * @return 
		 * 
		 */
		public function getRoleBitmap(resId : int) : Vector.<Vector.<Vector.<QhBitmap>>>
		{
			return roleDictionary[resId];
		}
		
		/**
		 * 检查是否存在缓存 
		 * @param key
		 * @return 
		 * 
		 */
		public function hasRoleBitmap(resId : int) : Boolean
		{
			return roleDictionary[resId] != null;
		}
		
		/**
		 * 清除缓存的所有临时位图数据 
		 * 
		 */
		public function cleanRoleBitmap(creature : Object) : void
		{
			for(var key : String in roleDictionary)
			{
				var resId : int = int(key);
				if(resId != creature.bodyResId && resId != creature.helmetResId && resId != creature.shoesResId
					&& resId != creature.armsResId && resId != creature.rideResid)//不是本地玩家的资源才进行删除
				{
					var vec : Vector.<Vector.<Vector.<QhBitmap>>> = roleDictionary[resId];
					for (var i:int = 0; i < vec.length; i++) 
					{
						if(vec[i] != null)
						{
							for (var j:int = 0; j < vec[i].length; j++) 
							{
								for (var k:int = 0; k < vec[i][j].length; k++) 
								{
									vec[i][j][k].destory();
									vec[i][j][k] = null;
								}
								vec[i][j] = null;
							}
							vec[i] = null;
						}
					}
					vec = null;
				}
				roleDictionary[resId] = null;
				delete roleDictionary[resId];
			}
//			for each (var vec : Vector.<Vector.<Vector.<QhBitmap>>> in roleDictionary) 
//			{
//				for (var i:int = 0; i < vec.length; i++) 
//				{
//					if(vec[i] != null)
//					{
//						for (var j:int = 0; j < vec[i].length; j++) 
//						{
//							for (var k:int = 0; k < vec[i][j].length; k++) 
//							{
//								vec[i][j][k].destory();
//								vec[i][j][k] = null;
//							}
//							vec[i][j] = null;
//						}
//						vec[i] = null;
//					}
//				}
//				vec = null;
//			}
//			roleDictionary = new Dictionary(true);
		}
		
		/**
		 * 取得QhBitmapSaveManager唯一实例 
		 * @return 
		 * 
		 */
		public static function get instance() : QhBitmapSaveManager
		{
			return _instance;
		}
	}
}